/*
 * SearchWidget.h
 *
 *  Created on: Jan 13, 2011
 *      Author: kmueller
 */

#ifndef SEARCHWIDGET_H_
#define SEARCHWIDGET_H_

#include "../logic/Settings.h"
#include "../logic/Helpers.h"

#include <QLineEdit>
#include <QDialog>
#include <QGeoCoordinate>
#include <QAbstractListModel>

class RouteWidget;
class QBoxLayout;
class QListView;
class QLabel;

class SearchLineEdit: public QLineEdit {
Q_OBJECT

public:
	SearchLineEdit(const QString& contents, QWidget * parent = 0);

private slots:
void changeContent();

private:
	QString content;
};

class FullOperationDialog: public QDialog {
	Q_OBJECT

	public:
		FullOperationDialog(LabeledCoordinate& coord, QWidget *parent);

		signals:
		void onMap(const LabeledCoordinate&);
		void asOrigin(const LabeledCoordinate&);
		void asDestination(const LabeledCoordinate&);

	private slots:
		void showOnMap();
		void useAsOrigin();
		void useAsDestination();

	private:
		LabeledCoordinate* coordinate;
	};

	class SmallOperationDialog: public QDialog {
	Q_OBJECT

	public:
		SmallOperationDialog(LabeledCoordinate& coord, QWidget *parent);

	signals:
	void use(const LabeledCoordinate&);

private slots:
	void use();

	private:
		LabeledCoordinate* coordinate;
	};

class SearchWidget: public QDialog {
Q_OBJECT

public:
	enum Type {
		NONE, ORIGIN, DESTINATION
	};

	SearchWidget(QWidget *parent = 0);
	SearchWidget(Type type, RouteWidget *parent = 0);
//	~SearchWidget();

	signals:
	void centeringMap(const LabeledCoordinate&);

private slots:
	void showOperations(const QModelIndex& index);
	void showOnMap(const LabeledCoordinate& coord);
	void useAs(const LabeledCoordinate& coord);
	void useAsOrigin(const LabeledCoordinate& coord);
	void useAsDestination(const LabeledCoordinate& coord);
	void updateSelectionBox(QAbstractListModel*);

	void backToCity();
	void selectCity(const QModelIndex& index);
	void search();

private:
	enum State {
		SEARCH_CITY, SELECT_CITY, SEARCH_STREET, SELECT_STREET
	};

	void init(Type type);
	void hideAll();
	void createComponents();
	void showSearchCity();
	void createSearchCity();
	void createSearchStreet();
	void showSearchStreet();
	void setState(State s);
	SearchWidget::State getState();
	void reassignConnections();
	void changeSelectionModel(QAbstractListModel* model);

	State state;
	Type type;
	SearchLineEdit *searchCity;
	SearchLineEdit *searchStreet;
	QPushButton* bSearch;
	QPushButton* bBack;
	QLabel* information;

	QListView *selection;

	int selectedCity;	// TODO: change to object
	LabeledCoordinate selectedCoordinate;
};

#endif /* SEARCHWIDGET_H_ */
